package cn.cloud9.nations;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @author OnCloud9
 * @description
 * @project RegionReptile
 * @date 2022年07月03日 下午 01:35
 */
public class WorldNationGet {


    /**
     * 官网的跳转逻辑
     * https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/yz_676205/1206_676207/1206x0_676209/
     *
     * @param baseUrl https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/yz_676205/
     * @param source ./1206_676207/
     * @return
     */
    private static String hrefJoinRule(String baseUrl, String source) {
        String replace = source.replace("./", "");
        final String detail = HttpUtil.get(baseUrl + replace);

        final Document redirectDoc = Jsoup.parse(detail);
        final Elements scriptList = redirectDoc.getElementsByTag("script");
        final String data = scriptList
                .stream()
                .filter(script -> script.data().contains("window.location.href"))
                .findFirst()
                .get().data();
        String substring = data.substring(data.indexOf("./"), data.length() - 3);
        substring = substring.replace("./", "");
        return baseUrl + replace + substring;
    }

    public static void main(String[] args) {
        final Db db = Db.use();

        String[] LINKS = {
            "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/yz_676205/",
            "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/fz_677316/",
            "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/oz_678770/",
            "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/bmz_679954/",
            "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/nmz_680924/",
            "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/dyz_681240/",
        };

        Arrays.asList(LINKS).forEach(link -> {
            final String htmlCode = HttpUtil.get(link);
            final Document document = Jsoup.parse(htmlCode);
            final Element linkList2 = document.getElementsByClass("linkList2").get(0);
            final String CONTINENT = linkList2.getElementsByClass("linkHd").get(0).text();

            linkList2.getElementsByTag("a").forEach(aTag -> {
                final String NATION_NAME = aTag.text();

                final String targetHref = hrefJoinRule(link, aTag.attr("href"));
                final String detailHtml = HttpUtil.get(targetHref);
                final Document detailDoc = Jsoup.parse(detailHtml);
                final Element content = detailDoc.getElementById("content");
                final Elements pList = content.getElementsByTag("p");
                String FULL_NAME = pList.get(0).text();
                final String DETAIL = content.text();
                try {
                    db.insert(Entity
                        .create("world_nations")
                        .set("NAME", NATION_NAME)
                        .set("LINK", targetHref)
                        .set("FULL_NAME", FULL_NAME)
                        .set("CONTINENT", CONTINENT)
                        .set("DETAIL", DETAIL)
                        .set("GEN_TIME", LocalDateTime.now()));
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            });

        });

    }
}
